home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / vol6n14.arc / PCMAP.ASM < prev    next >
Assembly Source File  |  1987-08-16  |  9KB  |  301 lines

  1. ;----------------------------------------------------------------------
  2. ;  PCMAP - Search through memory and print out the program names (for
  3. ;  DOS 3.x) and the number of bytes assigned to each in memory.
  4. ;----------------------------------------------------------------------
  5. CSEG    SEGMENT PARA PUBLIC 'CODE'        ;Start CODE segment
  6.     ASSUME    CS:CSEG,DS:CSEG,ES:CSEG,SS:CSEG    ;Set by DOS Loader
  7.         ORG    100H            ;COM file format
  8.  
  9. CR        EQU    0DH            ;Common equates
  10. LF        EQU    0AH
  11. TAB        EQU    09H
  12.  
  13. ENTPT:        JMP    PCMAP            ;Jump over data
  14.  
  15. ;======================================================================
  16. COPYRIGHT  DB  "PCMAP 1.0 (c) 1987, Ziff-Davis Publishing Corp."
  17. AUTHOR     DB  CR,LF,LF,'$',1AH,"Robert L. Hummel"
  18.  
  19. HEADING_MSG    DB    "Segment Paragraphs    Program"
  20. CR_LF_MSG    DB    CR,LF,'$'
  21. COM_MSG        DB    "COMMAND.COM"
  22. UNK_MSG        DB    "(Unknown)"
  23. FREE_MSG    DB    "(Free)"
  24. SPACE_MSG    DB    7 DUP(' '),'$'
  25. N_BLK        DB    0            ;Count table entries
  26. VER3        DB    0            ;=1 if Version 3.x
  27.  
  28. ;======================================================================
  29. PCMAP        PROC    NEAR
  30.  
  31.         MOV    DX,OFFSET COPYRIGHT
  32.         MOV    AH,9            ;Display string
  33.         INT    21H            ; Thru DOS
  34.  
  35.         MOV    AH,30H            ;Check DOS version
  36.         INT    21H            ; Thru DOS
  37.         CMP    AL,3            ;If not 3.x
  38.         JB    NOT_3            ; don't turn on flag
  39.         INC    VER3            ; else, indicate
  40. NOT_3:
  41. ;----------------------------------------------------------------------
  42. ;  Find the first MCB by scanning through memory.
  43. ;  On exit, ES points to the MCB.
  44. ;    AX=Block Address, BX=ES, CX=Owner, DX=Top of memory.
  45. ;----------------------------------------------------------------------
  46.         XOR    BX,BX            ;Zero BX
  47. SEARCH_MEM:
  48.         MOV    ES,BX            ;Point segment to 0
  49.     ASSUME ES:NOTHING            ;Tell the assembler
  50.  
  51.         CMP    BYTE PTR ES:[0],'M'    ;Is this a MCB?
  52.         JE    CHECK_MCB        ; might be
  53. CRAWL:
  54.         INC    BX            ;Point to next segment
  55.         JMP    SEARCH_MEM        ; continue search
  56. CHECK_MCB:
  57.         MOV    AX,BX            ;The MCB segment should
  58.         INC    AX            ; belong to the next segment
  59.         MOV    CX,WORD PTR ES:[1]    ;If owner of this block
  60.         CMP    AX,CX            ; is the same as the block
  61.         JNE    CRAWL            ; is first legitamate block
  62. FOUND_FIRST:
  63.         MOV    DX,WORD PTR DS:[2]    ;Top of memory
  64.  
  65. ;----------------------------------------------------------------------
  66. ;  If the owner=0, then this block is unallocated (free).
  67. ;    AX=Mem Address, BX=ES, CX=Owner, DX=Top of memory.
  68. ;  Look up OWNER in table.
  69. ;----------------------------------------------------------------------
  70. WORM_1:
  71.         CLD                ;String moves forward
  72.         MOV    DI,OFFSET TABLE        ;Where to look
  73.  
  74.         MOV    CX,WORD PTR ES:[1]    ;Get owner of this block
  75.         MOV    AL,N_BLK        ; number to look at
  76. SEARCH_TABLE:
  77.         OR    AL,AL            ;If no entries are left
  78.         JZ    CREATE_ENTRY        ; create a new one
  79.         DEC    AL            ;Adjust valid entries
  80.  
  81.         OR    CX,CX            ;If unallocated
  82.         JZ    SKIP_ENTRY        ; just skip to end
  83.  
  84.         MOV    SI,WORD PTR [DI]    ;Get PID of owner
  85.         CMP    CX,SI            ;If owner matches
  86.         JE    ADD_LEN            ; add more length
  87. SKIP_ENTRY:
  88.         ADD    DI,17            ;Point to next entry
  89.         JMP    SEARCH_TABLE
  90.  
  91. ;----------------------------------------------------------------------
  92. ;  Assume di points to 1st empty spot.
  93. ;----------------------------------------------------------------------
  94. CREATE_ENTRY:
  95.         INC    N_BLK            ;Adding new entry
  96.         MOV    AX,CX
  97.         OR    CX,CX            ;If block is free
  98.         JNZ    NOT_FREE
  99.         MOV    AX,BX            ;Put mem address as owner
  100.         INC    AX
  101. NOT_FREE:
  102.         MOV    WORD PTR [DI],AX    ;Put owner in table
  103.         MOV    WORD PTR [DI][2],0    ;Zero initial length
  104. ADD_LEN:
  105.         MOV    SI,WORD PTR ES:[3]    ;Block Length
  106.  
  107.         INC    BX            ;Use len to point bx...
  108.         MOV    AX,BX            ; (point AX to mem)
  109.         ADD    BX,SI            ;...to next MCB
  110.  
  111.         ADD    WORD PTR [DI][2],SI    ;Increase para count
  112.  
  113.         MOV    SI,OFFSET FREE_MSG    ;Memory is free
  114.         OR    CX,CX            ; if owner = 0
  115.         JNZ    HAVE_OWNER
  116.         MOV    CX,6            ;Length of string
  117.         JMP    COPY_NAME        ;Copy to TABLE
  118. HAVE_OWNER:
  119. ;----------------------------------------------------------------------
  120. ;  Is this block a primary (program) block?
  121. ;----------------------------------------------------------------------
  122.         CMP    AX,CX            ;Is mem = owner
  123.         JNE    FIND_NEXT        ;No
  124.  
  125. ;----------------------------------------------------------------------
  126. ;  This is a program block.  Word at offset 2Ch into the block contains
  127. ;  environment segment address.  (offset from current ES by 1 paragraph)
  128. ;----------------------------------------------------------------------
  129.         MOV    SI,WORD PTR ES:[2Ch+10h] ;Get env segment
  130.         CMP    N_BLK,1            ;If not first block
  131.         JNE    CHECK_ENV        ; look for environment
  132.  
  133.         MOV    SI,OFFSET COM_MSG    ;Put name in table
  134.         MOV    CX,11            ;String length
  135. COPY_NAME:
  136.         ADD    DI,4            ;String destination
  137.         PUSH    CS            ;Point ES to
  138.         POP    ES            ; this segment
  139.         REP    MOVSB            ;Move the bytes
  140.         MOV    AL,'$'            ;DOS string terminator
  141.         STOSB                ;Store it
  142.         PUSH    CS            ;Restore DS
  143.         POP    DS
  144.         JMP    SHORT FIND_NEXT
  145.  
  146. ;----------------------------------------------------------------------
  147. ;  Is the environment still allocated to the owner?
  148. ;----------------------------------------------------------------------
  149. CHECK_ENV:
  150.         CMP    VER3,0            ;If not 3.x
  151.         JE    NO_ENV            ; skip this section
  152.         DEC    SI            ;Point to env MCB
  153.         PUSH    SI            ;Put seg in DS
  154.         POP    DS
  155.         CMP    CX,WORD PTR DS:[1]    ;Compare owners
  156.         JNZ    NO_ENV            ;Not our property
  157.  
  158. ;----------------------------------------------------------------------
  159. ;  Point DS:SI to the environment and scan for the double zero entry.
  160. ;----------------------------------------------------------------------
  161.         INC    SI            ;Point SI to environment
  162.         PUSH    SI            ; and put in DS
  163.         POP    DS
  164.         XOR    SI,SI            ;DS:SI = ENV:0
  165.         INC    SI
  166. SCAN_ENV:
  167.         DEC    SI            ;Backup one byte
  168.         LODSW                ;Look at word
  169.         OR    AX,AX            ;If not double 0 byte
  170.         JNZ    SCAN_ENV        ;Continue to look
  171.  
  172. ;----------------------------------------------------------------------
  173. ;  Find the end of the program pathname.
  174. ;----------------------------------------------------------------------
  175.         LODSW                ;Skip a word (# strings)
  176.         MOV    BP,SI            ;Point to 1st char
  177.         DEC    BP
  178. SCAN_PATH:
  179.         LODSB                ;Read char at SI
  180.         OR    AL,AL            ;If 0, end of string
  181.         JNZ    SCAN_PATH        ; else continue reading
  182.  
  183. ;----------------------------------------------------------------------
  184. ;  SI points past the terminating 0.  Scan backwards for the \.
  185. ;----------------------------------------------------------------------
  186.         DEC    SI            ;Point SI to 0
  187.         MOV    CX,SI            ;Point CX past last char
  188. SCAN_NAME:
  189.         DEC    SI            ;Point to char
  190.         CMP    SI,BP            ;Is it 1st char?
  191.         JE    STRING_START
  192.         CMP    BYTE PTR [SI],'\'    ;It is backslash?
  193.         JNE    SCAN_NAME        ; no, continue
  194. STRING_START:
  195.         INC    SI            ;Point to start of string
  196.         SUB    CX,SI            ;Length of string
  197.         JMP    COPY_NAME        ;Transfer
  198. NO_ENV:
  199.         PUSH    CS            ;Restore DS
  200.         POP    DS
  201.         MOV    CX,9            ;Number of chars
  202.         MOV    SI,OFFSET UNK_MSG    ;Unknown
  203.         JMP    COPY_NAME        ;Transfer
  204.  
  205. ;----------------------------------------------------------------------
  206. ;  Point ES to next MCB and continue search.  Stop at top of memory.
  207. ;----------------------------------------------------------------------
  208. FIND_NEXT:
  209.         MOV    ES,BX            ;Set ES to next paragraph
  210.         CMP    BX,DX            ;At top of memory?
  211.         JE    NO_MORE            ; then done
  212.         JMP    WORM_1            ;else continue
  213. NO_MORE:
  214. ;----------------------------------------------------------------------
  215. ;  Display the resulting table on the screen.
  216. ;----------------------------------------------------------------------
  217.         MOV    DX,OFFSET HEADING_MSG    ;Display the heading
  218.         MOV    AH,9            ;Display string
  219.         INT    21H            ; Thru DOS
  220.  
  221.         MOV    SI,OFFSET TABLE        ;Table location
  222.         MOV    CL,N_BLK        ;Number of entries
  223.         XOR    CH,CH            ; as a word
  224. PRINT_TABLE:
  225.         CALL    PRINT_WORD        ;Print owner
  226.         CALL    PRINT_WORD        ;and size
  227.  
  228.         MOV    DX,SI            ;Name of owner
  229.         MOV    AH,9            ;Display string
  230.         INT    21H            ; Thru DOS
  231.         ADD    SI,13            ;Point to next entry
  232.         MOV    DX,OFFSET CR_LF_MSG    ;Newline
  233.         MOV    AH,9            ;Display string
  234.         INT    21H            ; Thru DOS
  235.         LOOP    PRINT_TABLE
  236.  
  237.         MOV    AH,4CH            ;Terminate program
  238.         INT    21H            ;Thru DOS
  239.  
  240. PCMAP        ENDP
  241.  
  242. ;======================================================================
  243. PRINT_WORD    PROC    NEAR
  244.  
  245.         LODSW                ;Get owner
  246.         CALL    HEX4            ;Write 4 digits
  247.         MOV    DX,OFFSET SPACE_MSG    ;space over
  248.         MOV    AH,9            ;Display string
  249.         INT    21H            ; Thru DOS
  250.         RET
  251.  
  252. PRINT_WORD    ENDP
  253.  
  254. ;======================================================================
  255. ;  HEX4 - Write AX as 4 hex digits to console
  256. ;  HEX2 - Write AL as 2 hex digits to console
  257. ;-----------------------------------------------------------------------------
  258. HEX4        PROC    NEAR
  259.  
  260.         PUSH    AX            ;Save register
  261.         MOV    AL,AH            ;Show high digits first
  262.         CALL    HEX2            ;Display AL
  263.         POP    AX            ;Restore low digits in AL
  264.  
  265. HEX2        PROC    NEAR            ;Display AL
  266.  
  267.         PUSH    AX            ;Save register
  268.         PUSH    CX            ;Save CX during shift
  269.         MOV    CL,4
  270.         SHR    AL,CL            ;Get high 4 bits
  271.         POP    CX            ;Restore CX
  272.  
  273.         CALL    H2C            ;Display upper AL digit
  274.         POP    AX            ;Restore lower
  275.         AND    AL,0FH            ;Mask and display
  276. H2C:
  277.         ADD    AL,90H            ;Convert AL to ASCII
  278.         DAA
  279.         ADC    AL,40H
  280.         DAA
  281.  
  282.         PUSH    DX            ;Save register during call
  283.         MOV    DL,AL            ;Character in DL
  284.         MOV    AH,2            ;Display Output Fn
  285.         INT    21H            ; Thru DOS
  286.         POP    DX            ;Restore register
  287.  
  288.         RET
  289.  
  290. HEX2        ENDP
  291. HEX4        ENDP
  292.  
  293. ;======================================================================
  294. ;  Format is TABLE  DB  20 DUP (0,0, 0,0,"             ")
  295. ;----------------------------------------------------------------------
  296. TABLE        LABEL    BYTE
  297.  
  298. ;----------------------------------------------------------------------
  299. CSEG    ENDS
  300.     END    ENTPT
  301.